package com.allinpay.merchantweb.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.allinpay.merchantweb.common.utils.HttpClientWrapper;
import com.allinpay.merchantweb.common.utils.StringUtil;
import com.allinpay.merchantweb.service.EncodeAndDecodeService;
import com.google.gson.Gson;

@Service
public class EncodeAndDecodeServiceImpl implements EncodeAndDecodeService {

    private Logger logger = LoggerFactory.getLogger(EncodeAndDecodeServiceImpl.class);

    @Value("${tlhy.securityservice.url}")
    private String SECURITY_SERVICE_URL;

    /*
     * 私钥签名
     */
    @Override
    @SuppressWarnings({ "unchecked" })
    public String sign(String data) {
        Map<String, Object> map = new HashMap();
        map.put("YWMA", "0001");
        map.put("JYMA", "0002");
        map.put("CZMA", "0001");
        map.put("JRDM", "1111");
        map.put("MTHD", "signByTLPK");
        map.put("CZLY", "USER");        
        map.put("JMYC", data);
        map.put("PKID", "002");
        Gson gson = new Gson();
        String jsonMap = gson.toJson(map);
        Map<String, Object> map2 = new HashMap();
        map2.put("MSG", jsonMap);

        String httpResultStr = HttpClientWrapper.doPost(SECURITY_SERVICE_URL, map2);
        Map retMap = gson.fromJson(httpResultStr, Map.class);
        String result = (String) retMap.get("RTVE");
        if (StringUtil.isEmpty(result)) {
            logger.error("调用私钥签名失败，请求报文为：{}，返回报文为：{}", map2, retMap);
        }
        return result;
    }

   
}
